package com.zr.repertory.affiliation.controller;

import com.zr.repertory.affiliation.model.pojo.*;
import com.zr.repertory.affiliation.service.WareAreaService;
import com.zr.repertory.affiliation.service.WareHouseBelongService;
import com.zr.repertory.affiliation.service.WareHouseService;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName LegalPersonController
 * @Description TODO 法人的查询
 * @Author 马家奎
 * @Date 2019/11/20 19:46
 */
@RestController
@CrossOrigin
@RequestMapping("CompanyStorage")
public class WareHouseBelongController {

    @Autowired
    private WareHouseService wareHouseService;
    @Autowired
    private WareAreaService wareAreaService;

    @Autowired
    private WareHouseBelongService wareHouseBelongService;

    /**
     * 分页查询
     * @param belongPageVo
     * @return
     */
    @PostMapping("queryPage")
    public ResultUtil queryPage(@RequestBody BelongPageVo belongPageVo){

        PageUtil<WareHouseBelong> page = wareHouseBelongService.queryPage(belongPageVo);
        return ResultBuilderUtil.success(page);
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("queryCompanyStorageById")
    public ResultUtil see(Integer id){

        BelongSeeResultVo seeResultVo = wareHouseBelongService.see(id);

        return ResultBuilderUtil.success(seeResultVo);
    }

    /**
     * 查询所有库房
     * @param queryWareHouse
     * @return
     */
    @PostMapping("queryWarehouse")
    public ResultUtil queryAllWareHouse(QueryWareHouse queryWareHouse){

        List<WareHouse> wareHouseList = wareHouseService.queryAllWareHouse(queryWareHouse);

        return ResultBuilderUtil.success(wareHouseList);
    }

    /**
     * 根据法人id查询该法人下所有库区信息
     * @param legalPersonId
     * @return
     */
    @GetMapping("queryStorageSectionByLegalPersonId")
    public ResultUtil queryStorageSectionByLegalPersonId(Integer legalPersonId){
        List<WareArea> wareAreaList = wareAreaService.queryStorageSectionByLegalPersonId(legalPersonId);

        System.out.println("所有库区信息"+wareAreaList);
        return ResultBuilderUtil.success(wareAreaList);
    }

    /**
     * 查询选中库房下的库区明细列表
     * @return
     */
    @PostMapping("queryStorageSection")
    public ResultUtil queryStorageSection(@RequestBody StorageSectionVo storageSectionVo){

        List<WareArea> wareAreaList = wareAreaService.queryStorageSection(storageSectionVo);
        System.out.println("查询选中库房下的库区明细列表"+wareAreaList);
        return ResultBuilderUtil.success(wareAreaList);
    }


    /**
     * 添加
     * @param addBelongVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public ResultUtil add(@RequestBody @Valid AddBelongVo addBelongVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }else {

            return wareHouseBelongService.add(addBelongVo);
        }
        
    }
}
